package pacBioSimple;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class PacBioOverlapRec {

	
	
	public void findOverlappAndCut(PacBioReadContainer read) throws IOException, InterruptedException
	{
		StringBuilder toScan = read.getOrginalSequence();
		//System.out.println(toScan);
		
		
		//first we have to call lastz
		PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("temp_input.fa")));
		pw.println(read.getHeader());
		pw.println(toScan.toString());
		pw.flush();
		pw.close();
		
		java.lang.Runtime rt = java.lang.Runtime.getRuntime();
		
		java.lang.Process p = rt.exec("lastz temp_input.fa temp_input.fa --format=text --gapped --nogfextend --notrivial");
			
		ArrayList<StringBuilder> subReads = new ArrayList<StringBuilder>();
			subReads.add(toScan);
			//now we have to execute msa
			//create first a fasta file!
			//System.out.println(subReads.toString());
			
			int longest_subread=0;
			int longest_subread_pos=0;
			if(subReads.size()>1)
			{
					
				 pw = new PrintWriter(new BufferedWriter(new FileWriter("temp_input.fasta")));
				
				for(int i=0; i <subReads.size();++i)
				{
					pw.println(">"+i);
					pw.println(subReads.get(i));
					if(longest_subread<subReads.get(i).length())
					{
						longest_subread=subReads.get(i).length();
						longest_subread_pos=i;
					}
				}
				pw.flush();
				pw.close();
				//call mafft
				rt = java.lang.Runtime.getRuntime();
				// Start a new process: UNIX command ls
		        p = rt.exec("/projects/dep_coupland/grp_nordstrom/bin/mafft/bin/mafft --preservecase --adjustdirectionaccurately  --ep 0.123 --quiet temp_input.fasta");
		        // You can or maybe should wait for the process to complete
		        p.waitFor();
		       // System.out.println("Process exited with code = " + p.exitValue());
		        // Get process' output: its InputStream
		        java.io.InputStream is = p.getInputStream();
		        java.io.BufferedReader reader = new java.io.BufferedReader(new InputStreamReader(is));
		        // And print each line
		        String s = null;
		        PacBioFastaMSAContainer msaContainer = new PacBioFastaMSAContainer();
		        
		        boolean found=false;
		        StringBuilder seq = new StringBuilder();
		        while ((s = reader.readLine()) != null) 
		        {
		        	//System.out.println(s);
		        	if(s.startsWith(">"))
		        	{
		        		if(found==true)
		        		{
		        			msaContainer.readFastaFile(seq.toString());
		        			seq = new StringBuilder();
		        		}
		        		else
		        		{
		        			found=true;
		        		}
		        		seq.append(reader.readLine());
		        	}
		        	else if(found==true)
		        	{
		        		seq.append(s);
		        	}
		        }
		        if(seq.length()>0)
		        {
		    		msaContainer.readFastaFile(seq.toString());
		        }
		        is.close();
		        int conv=msaContainer.getMaximumNumberConservation(90.0);
		        StringBuilder sequence = new StringBuilder();
		        if(conv>10)
		        {
		        	sequence = msaContainer.getConsensusSequence();
		        }
		        else
		        {
		        	//get the longest one
		        	sequence=subReads.get(longest_subread_pos);	        	
		        }
		        read.setOrginalSequence(sequence);
			}
			else
			{
				read.setOrginalSequence(subReads.get(0));
			}
		}
	}

